package com.applet.applet_shangcheng.service;

import com.applet.applet_shangcheng.dao.AnnouncementMapper;
import com.applet.applet_shangcheng.utlis.DateUtils;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.applet.applet_shangcheng.entity.Announcement;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional
public class AnnouncementService {
    @Autowired
    private AnnouncementMapper announcementMapper;


    public Announcement findAnnouncement(int id) {
        Announcement announcementList = announcementMapper.findAnnouncement(id);
        return announcementList;
    }

    public int insertAnnouncement(Announcement announcement) {
        //System.out.println(announcement.getContent());
        // announcement.setStatus(1);
        announcement.setCreateDate(new Date());
        int result = announcementMapper.insertAnnouncement(announcement);
        return result;
    }

    public int updateStatus(int id, int status) {
        int result = announcementMapper.updateStatus(id, status);
        return result;
    }

    public int deleteAnnouncement(int id) {
        int result = announcementMapper.deleteAnnouncement(id);
        return result;
    }

    public int updateAnnouncement(Announcement announcement) {
        announcement.setCreateDate(new Date());
        int result = announcementMapper.updateAnnouncement(announcement);
        return result;
    }

    public PageResult findAll(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Announcement> page = announcementMapper.findAll(queryString);
        for (Announcement announcement : page) {
            Date createDate = announcement.getCreateDate();
            try {
                String date2String = DateUtils.parseDate2String(createDate);
                announcement.setCreateDate1(date2String);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PageInfo<Announcement> pageInfo = new PageInfo<>(page);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    public List<Announcement> findAllS() {
        List<Announcement> announcementList = announcementMapper.findAllS();
        return announcementList;
    }
}
